package sk.fiit.vppj.eventmanager.core;

import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;



/**
 * Class provides the whole application with databse connectivity.
 * @author Marek
 *
 */
public final class DatabaseManager {
	//using singleton pattern
	private final static DatabaseManager dbManager = new DatabaseManager();
	private Properties config = null;
	private JdbcTemplate jdbcTemplate;

	/**
	 * Setting neccessary data from property file (or config.xml if is available) for connection to database
	 */
	private DatabaseManager() {
		
		this.config = new ConfigManager().getProperties();
		MysqlDataSource dataSource = new MysqlDataSource();
		dataSource.setUser( config.getProperty( "dbuser" ) );
		dataSource.setPassword( config.getProperty( "dbpass" ) );
		dataSource.setServerName( config.getProperty( "dbhost" ) );
		dataSource.setDatabaseName( config.getProperty( "dbname" ) );
		dataSource.setZeroDateTimeBehavior( "convertToNull" );
		this.jdbcTemplate = new JdbcTemplate( dataSource );
	}

	/**
	 * Method provides jdbcTemplate for the whole application
	 * @return jdbcTemplate - for more read specification of Spring 3.0.5
	 */
	public JdbcTemplate getJdbcTemplate()  {
		return this.jdbcTemplate;
	}

	/**
	 * 
	 * @return instance of this static class
	 */
	public static DatabaseManager getInstance() {
		return dbManager;
	}

}
